| Conditions | 4 |
| Paths | 16385 |
| Total Lines | 167 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // Copyright © 2017 Tangdongxin |
||
| 25 | function onConfig (result) { |
||
| 26 | |||
| 27 | if (!result) { // {{{ |
||
| 28 | |||
| 29 | dlog('No Config find'); |
||
| 30 | fontStyle = 'Consolas'; |
||
| 31 | fontSize = '14px'; |
||
| 32 | bgColor = '#FDF6E3'; |
||
| 33 | intColor = '#657A81'; |
||
| 34 | strColor = '#2AA198'; |
||
| 35 | keyColor = '#B58900'; |
||
| 36 | defaultColor = '#586E75'; |
||
| 37 | |||
| 38 | strictOnly = false; |
||
| 39 | hideDetails = false; |
||
| 40 | dontBeatify = false; |
||
| 41 | isHighlight = false; |
||
| 42 | isDebug = false; |
||
| 43 | isRelaxedJsonSupport = false; |
||
| 44 | |||
| 45 | strLength = 300; |
||
| 46 | return; |
||
| 47 | |||
| 48 | }// }}} |
||
| 49 | |||
| 50 | if (result && result[0]) { // {{{ |
||
| 51 | |||
| 52 | fontStyle = result[0].fontStyle || 'Consolas'; |
||
| 53 | fontSize = result[0].fontSize || '14px'; |
||
| 54 | bgColor = result[0].bgColor || '#FDF6E3'; |
||
| 55 | intColor = result[0].intColor || '#657A81'; |
||
| 56 | strColor = result[0].strColor || '#2AA198'; |
||
| 57 | keyColor = result[0].keyColor || '#B58900'; |
||
| 58 | defaultColor = result[0].defaultColor || '#586E75'; |
||
| 59 | |||
| 60 | strictOnly = result[0].strictOnly || false; |
||
| 61 | hideDetails = result[0].hideDetails || false; |
||
| 62 | dontBeatify = result[0].dontBeatify || false; |
||
| 63 | isHighlight = result[0].isHighlight || false; |
||
| 64 | isDebug = result[0].isDebug || false; |
||
| 65 | isRelaxedJsonSupport = result[0].isRelaxedJsonSupport || false; |
||
| 66 | strLength = result[0].strLength || 300; |
||
| 67 | |||
| 68 | } else { |
||
| 69 | |||
| 70 | fontStyle = result.fontStyle || 'Consolas'; |
||
| 71 | fontSize = result.fontSize || '14px'; |
||
| 72 | bgColor = result.bgColor || '#FDF6E3'; |
||
| 73 | intColor = result.intColor || '#657A81'; |
||
| 74 | strColor = result.strColor || '#2AA198'; |
||
| 75 | keyColor = result.keyColor || '#B58900'; |
||
| 76 | defaultColor = result.defaultColor || '#586E75'; |
||
| 77 | |||
| 78 | strictOnly = result.strictOnly || false; |
||
| 79 | hideDetails = result.hideDetails || false; |
||
| 80 | dontBeatify = result.dontBeatify || false; |
||
| 81 | isHighlight = result.isHighlight || false; |
||
| 82 | isDebug = result.isDebug || false; |
||
| 83 | isRelaxedJsonSupport = result.isRelaxedJsonSupport || false; |
||
| 84 | strLength = result.strLength || 300; |
||
| 85 | |||
| 86 | }// }}} |
||
| 87 | |||
| 88 | document.addEventListener('click', function (e) { |
||
| 89 | |||
| 90 | dlog(`is the plugin do job: ${isJsonThisDocument}`); |
||
| 91 | if (!isJsonThisDocument) { |
||
| 92 | |||
| 93 | return; |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | dlog(e); |
||
| 98 | |||
| 99 | const target = e.target; |
||
| 100 | if (target.tagName.toUpperCase() == 'I') { // {{{ |
||
| 101 | |||
| 102 | const isClose = target.classList.contains(COLL); |
||
| 103 | const classname = target.classList[0]; |
||
| 104 | if (isClose) { |
||
| 105 | |||
| 106 | target.removeAttribute('class'); |
||
| 107 | target.setAttribute('class', classname); |
||
| 108 | |||
| 109 | } else { |
||
| 110 | |||
| 111 | target.removeAttribute('class'); |
||
| 112 | target.setAttribute('class', `${classname } C${ classname.substring(1)}`); |
||
| 113 | |||
| 114 | } |
||
| 115 | e.preventDefault(); |
||
| 116 | // }}} |
||
| 117 | |||
| 118 | } else if (target.tagName.toUpperCase() == 'STR') { // {{{ |
||
| 119 | |||
| 120 | var parentElement = target.parentElement; |
||
| 121 | const originStr = parentElement.innerHTML; |
||
| 122 | parentElement.innerHTML = parentElement.getAttribute('content'); |
||
| 123 | parentElement.setAttribute('content', originStr); |
||
| 124 | // }}} |
||
| 125 | |||
| 126 | } else if (target.tagName.toUpperCase() == 'JSON') { // {{{ |
||
| 127 | |||
| 128 | if (document.getElementById('light')) { |
||
| 129 | |||
| 130 | var lightDiv = document.getElementById('light'); |
||
| 131 | lightDiv.parentElement.removeChild(lightDiv); |
||
| 132 | |||
| 133 | } |
||
| 134 | if (document.getElementById('fade')) { |
||
| 135 | |||
| 136 | var fadeDiv = document.getElementById('fade'); |
||
| 137 | fadeDiv.parentElement.removeChild(fadeDiv); |
||
| 138 | |||
| 139 | } |
||
| 140 | |||
| 141 | var parentElement = target.parentElement; |
||
| 142 | |||
| 143 | var fadeDiv = document.createElement('div'); |
||
| 144 | fadeDiv.setAttribute('id', 'fade'); |
||
| 145 | fadeDiv.classList.add('black_overlay'); |
||
| 146 | document.body.appendChild(fadeDiv); |
||
| 147 | |||
| 148 | var lightDiv = document.createElement('div'); |
||
| 149 | lightDiv.setAttribute('id', 'light'); |
||
| 150 | lightDiv.classList.add('white_content'); |
||
| 151 | document.body.appendChild(lightDiv); |
||
| 152 | |||
| 153 | document.getElementById('light').style.display = 'block'; |
||
| 154 | document.getElementById('fade').style.display = 'block'; |
||
| 155 | draw(eval(parentElement.getAttribute('json')), lightDiv); |
||
| 156 | // }}} |
||
| 157 | |||
| 158 | } else if (target.classList.contains('black_overlay')) { // {{{ |
||
| 159 | |||
| 160 | var fadeDiv = document.getElementById('fade'); |
||
| 161 | fadeDiv.parentElement.removeChild(fadeDiv); |
||
| 162 | var lightDiv = document.getElementById('light'); |
||
| 163 | lightDiv.parentElement.removeChild(lightDiv); |
||
| 164 | // }}} |
||
| 165 | |||
| 166 | } else if (target.className.toUpperCase() == DIV.toUpperCase()) { // {{{ |
||
| 167 | |||
| 168 | if (isHighlight) { |
||
| 169 | |||
| 170 | if (target.style.borderLeft) { |
||
| 171 | |||
| 172 | target.removeAttribute('style'); |
||
| 173 | |||
| 174 | } else { |
||
| 175 | |||
| 176 | target.style = 'border-left:1px solid'; |
||
| 177 | |||
| 178 | } |
||
| 179 | |||
| 180 | } |
||
| 181 | // }}} |
||
| 182 | |||
| 183 | } else if (target.tagName.toUpperCase() === 'URL') { // {{{ |
||
| 184 | |||
| 185 | url = target.parentElement.getAttribute('url'); |
||
| 186 | dlog(`go:${ url}`); |
||
| 187 | window.open(url); |
||
| 188 | // }}} |
||
| 189 | |||
| 190 | } |
||
| 191 | |||
| 192 | }, true); |
||
| 195 |